package com.team.arms.public_data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.public_data.domain.ListSurveyItems;
import com.team.arms.public_data.domain.SurveyOptionInformation;
import com.team.arms.public_data.domain.VisitSurveyConfiguration;
import com.team.arms.public_data.service.VisitSurveyConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yjh
 * @create 2022-06-05-19:49
 * @description:
 */

@Api( tags = "走访调查配置")
@RestController
@RequestMapping("/publicDate/VisitSurveyConfiguration")
public class VisitSurveyConfigurationController extends BaseController {

    @Autowired
    private VisitSurveyConfigurationService visitSurveyConfigurationService;

    /**
     * 查询走访调查配置
     * @return
     */
    @ApiOperation("查询走访调查配置")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<VisitSurveyConfiguration> list = visitSurveyConfigurationService.list();
        PageInfo<VisitSurveyConfiguration> pageInfo = new PageInfo<VisitSurveyConfiguration>(list);
        PageVO<VisitSurveyConfiguration> visitSurveyConfigurationPageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, visitSurveyConfigurationPageVO);
        visitSurveyConfigurationPageVO.setResult(pageInfo.getList());
        visitSurveyConfigurationPageVO.setPage(pageNum);
        return AjaxResult.success(visitSurveyConfigurationPageVO);
    }

    @ApiOperation("根据id查询走访调查配置")
    @GetMapping("/selectById")
    public AjaxResult selectById( @RequestParam(value = "visits_surveys_id")String id){
        return AjaxResult.success(visitSurveyConfigurationService.selectAllByVisitsSurveysId(id));
    }

    /**
     *
     * @param field
     * @param content
     * @return
     */
    @ApiOperation("根据对应字段查询资料清单信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        return AjaxResult.success(visitSurveyConfigurationService.selectByFiled(field,content));
    }

    /**
     * 新增走访调查配置
     */
    @ApiOperation("新增走访调查配置")
    @PostMapping("/install")
    public AjaxResult install(@RequestBody VisitSurveyConfiguration visitSurveyConfiguration){
        return toAjax(visitSurveyConfigurationService.insertAll(visitSurveyConfiguration));
    }

    /**
     * 修改走访调查配置
     */
    @ApiOperation("修改走访调查配置")
    @Log(title = "修改走访调查配置", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateInformation(
            @RequestParam(value = "visits_surveys_id")String id,
            @RequestBody VisitSurveyConfiguration visitSurveyConfiguration){
        QueryWrapper<VisitSurveyConfiguration> wrapper = new QueryWrapper();
        wrapper.eq("visits_surveys_id",id);
        VisitSurveyConfiguration configuration = visitSurveyConfigurationService.getById(id);
        return toAjax(visitSurveyConfigurationService.update(visitSurveyConfiguration,wrapper));
    }

    /**
     * 配置走访调查
     * @param listSurveyItems
     * @param surveyOptionInformation
     */
    @ApiOperation("配置走访调查")
    @PostMapping("/Configuration")
    public AjaxResult configuration(@RequestBody ListSurveyItems[] listSurveyItems,
                                    @RequestBody SurveyOptionInformation[] surveyOptionInformation){
        return toAjax(visitSurveyConfigurationService.configuration(listSurveyItems, surveyOptionInformation));
    }

}
